package com.fms.trollalarm.fragment;

import java.util.Random;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.fms.trollalarm.R;
import com.fms.trollalarm.object.LevelType;
import com.fms.trollalarm.object.TrollType;
import com.fms.trollalarm.utils.Common;

public class MathFragment extends AlarmFragment {

	// Layout
	private TextView tvMathContent;
	private EditText etAnswer;
	private Button btAnswer;
	private Button btSnooze;
	private Button btChange;
	private String[] question;
	private int[] answer;
	private int answerKey;

	public static MathFragment newInstance(int levelType) {
		MathFragment fragment = new MathFragment();
		Bundle bundle = new Bundle();
		bundle.putInt(Common.IntentExtra.TROLL_LEVEL, levelType);
		fragment.setArguments(bundle);
		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_math, null);
		findViewIDs(v);
		init();
		setEventView();
		return v;
	}

	private void setEventView() {
		etAnswer.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				if (s.toString().trim().length() > 0) {
					btAnswer.setEnabled(true);
				} else {
					btAnswer.setEnabled(false);
				}
			}
		});
		btAnswer.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String text = etAnswer.getText().toString();
				if (Integer.parseInt(text) == answer[answerKey]) {
					setOnStopAlarm();
				}
				etAnswer.setText("");
			}
		});
		btSnooze.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setOnSnoozeAlarm();
			}
		});

		btChange.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setOnChangeTrollType(TrollType.MATH);
			}
		});
	}

	private void init() {
		setTrollLevel();
		Random rnd = new Random();
		answerKey = rnd.nextInt(question.length);
		tvMathContent.setText(question[answerKey]);
		btAnswer.setEnabled(false);
	}

	private void setTrollLevel() {
		int level = getTrollLevel();
		if (level == LevelType.EASY.ordinal()) {
			question = getResources().getStringArray(R.array.math_array_easy);
			answer = getResources().getIntArray(R.array.math_answer_array_easy);
		} else if (level == LevelType.MEDIUM.ordinal()) {
			question = getResources().getStringArray(R.array.math_array_medium);
			answer = getResources().getIntArray(
					R.array.math_answer_array_medium);
		} else if (level == LevelType.HARD.ordinal()) {
			question = getResources().getStringArray(R.array.math_array_hard);
			answer = getResources().getIntArray(R.array.math_answer_array_hard);
		}

	}

	private void findViewIDs(View v) {
		tvMathContent = (TextView) v.findViewById(R.id.tv_math_content);
		etAnswer = (EditText) v.findViewById(R.id.et_answer);
		btAnswer = (Button) v.findViewById(R.id.bt_answer);
		btSnooze = (Button) v.findViewById(R.id.bt_snooze);
		btChange = (Button) v.findViewById(R.id.bt_change);
	}
}
